# 设置项目根目录
LVGL_DIR := ${shell pwd}
LVGL_DIR_NAME ?= lvgl
BIN ?= lvgl-demo

include lvgl/lvgl.mk
include lv_drivers/lv_drivers.mk

# 源文件列表
CSRCS += main.c test.c

# 构建目录
BUILD_DIR := $(LVGL_DIR)/build/
OBJ_DIR = $(BUILD_DIR)/obj/
BIN_DIR = $(BUILD_DIR)/bin/

# 编译目标
OBJEXT ?= .o
AOBJS = $(addprefix $(OBJ_DIR),$(ASRCS:.S=$(OBJEXT)))
COBJS = $(addprefix $(OBJ_DIR),$(CSRCS:.c=$(OBJEXT)))
DEPS = $(addprefix $(OBJ_DIR),$(CSRCS:.c=.d))

# 源文件和对象文件
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)

.PHONY: all env clean default
all: env default

-include $(DEPS)

env:
	@echo "Building env......."
	@if [ ! -d "$(BUILD_DIR)" ]; then mkdir -p $(BUILD_DIR); fi
	@if [ ! -d "$(OBJ_DIR)" ]; then mkdir -p $(OBJ_DIR); fi
	@if [ ! -d "$(BIN_DIR)" ]; then mkdir -p $(BIN_DIR); fi

$(OBJ_DIR)main.o: main.c
	@echo "Compiling $(LVGL_DIR)/$<"
	@$(CC)  $(CFLAGS) -c $< -o $@

$(OBJ_DIR)%.o: %.c
	@echo "Compiling 11 $(LVGL_DIR) $<"
	@mkdir -p $(OBJ_DIR)$(dir $<)
	@$(CC)  $(CFLAGS) -MMD -c $< -o $@

default: $(OBJS)
	@echo "Linking $(BIN)"
	@$(CC) -o $(BIN_DIR)$(BIN) $(OBJS) $(LDFLAGS) $(LIBS)

clean:
	rm build -r

